<?php

namespace app\home\controller;

use think\Controller;
use think\Loader;
use think\Request;
use think\Jump;

class Login extends Controller
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if (Request::instance()->isGet()){
            $this->assign('HomeLoginErrorInfo', session('HomeLoginErrorInfo'));
            return view();
        }
        if (Request::instance()->isPost()){
            $user = input('post.email');
            $pwd = input('post.password');
            (!$user) && $this->LoginErrorRedirect('邮箱不能为空');
            (!$pwd) && $this->LoginErrorRedirect('密码不能为空');
            $this->checkLogin($user, $pwd);
        }


    }
    public function checkLogin($user, $pwd){

        $userInfo = Loader::model('User')->getUserInfo($user);
        empty($userInfo) && $this->LoginErrorRedirect('用户不存在');

        if (!password_verify($pwd, $userInfo['password'])){
            $this->LoginErrorRedirect('密码错误');
        }
        $user_Info['user']=[
            'uid'=>$userInfo['id'],
            'nickname'=>$userInfo['nickname']
        ];
        session('UserXinxi', $user_Info);
        session('HomeLoginErrorInfo',null);
        $this->redirect('home/index/index');
    }
    public function LoginErrorRedirect($error, $url = 'home/Login/index'){
        session('HomeLoginErrorInfo', $error);
        $this->redirect($url);
    }
}
